package MyGui;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import DistSys.*;
import DataAccess.*;
/**
 * Title:        iPAD
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      ABB Inc.
 * @author Fangxing Li
 * @version 1.0
 */

public class ImportZonesDlg extends MyDlgFrame implements ActionListener{

  JTextField  tfFileName;
  JTextField  tfZone;
  JLabel      lblFileName, lblZone;
  JButton     btnSelect, btnImport;

  public ImportZonesDlg(MainFrame parent, String title) {
      super(parent, title);
      init();
  }

  private void init(){
      JPanel pnlTop = new JPanel();
      pnlTop.setBorder( new TitledBorder("Import a zone") );
      GridBagLayout gridBag = new GridBagLayout();
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = new Insets(5,5,5,5);
      pnlTop.setLayout( gridBag );

      lblFileName=new JLabel("File(*.pad)");
      lblFileName.setFont( GuiConst.fontDefault);
      lblZone = new JLabel("Zone(Entire System=-1)");
      lblZone.setFont( GuiConst.fontDefault);
      tfFileName=new JTextField(24);
      tfFileName.setFont( GuiConst.fontDefault);
      //cmbZone = new ZoneComboBox();
      //cmbZone.setFont( GuiConst.fontDefault);
      tfZone=new JTextField("", 6 );
      tfZone.setFont( GuiConst.fontDefault);
      tfZone.setInputVerifier( new DoubleVerifier() );
      gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=4; pnlTop.add(lblFileName, gbc);
      gbc.gridx=4; gbc.gridy=0; gbc.gridwidth=1; pnlTop.add(lblZone, gbc);
      gbc.gridx=0; gbc.gridy=1; gbc.gridwidth=4; pnlTop.add(tfFileName, gbc);
      gbc.gridx=4; gbc.gridy=1; gbc.gridwidth=1; pnlTop.add(tfZone, gbc);

      JPanel pnlBottom = new JPanel ();
      btnSelect = new JButton("Choose File");
      btnSelect.setFont( GuiConst.fontDefault);
      btnImport = new JButton("Import");
      btnImport.setFont( GuiConst.fontDefault);
      pnlBottom.add( btnSelect);
      pnlBottom.add( btnImport);

      this.getContentPane().setLayout( new BorderLayout() );
      this.getContentPane().add( pnlTop, BorderLayout.CENTER);
      this.getContentPane().add( pnlBottom, BorderLayout.SOUTH);

      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      this.setSize(350,150);
      this.setLocation(200,200);

      btnSelect.addActionListener( this );
      btnImport.addActionListener( this );
  }

  public void actionPerformed( ActionEvent evt ){
      Object src = evt.getSource();
      if( src == btnSelect ){
          JFileChooser chooser = mainFrame.menuBar.fileChooser;
          chooser.setCurrentDirectory(Sys.personalInfo.getProjectsPath());
          chooser.setDialogTitle("Open a Project of MS Access format");
          chooser.resetChoosableFileFilters();
          //chooser.addChoosableFileFilter( new AllCompatiableFileFilter()); // show *.mdb, *.pad
          //chooser.addChoosableFileFilter( new MdbFileFilter());
          chooser.addChoosableFileFilter( new PadFileFilter());
          chooser.setAcceptAllFileFilterUsed(false);

          // get file name
          File importedFile;
          int returnVal = chooser.showOpenDialog(this);
          if (returnVal == JFileChooser.APPROVE_OPTION) {
              importedFile = chooser.getSelectedFile();
          } else // select cancel button
              return;
          tfFileName.setText( importedFile.getPath() );
          tfZone.setText("-1");
          tfZone.selectAll();
      }else if( src == btnImport ){
          try {
              int cntr=0;
              //int zone = cmbZone.getSelectedZone();
              double dZone = Double.parseDouble( tfZone.getText());
              String fileName = tfFileName.getText();
              mainFrame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
              if( fileName.endsWith(".pad")){
                  BinaryFileAccessor bfa = new BinaryFileAccessor();
                  cntr = bfa.importZoneFromProject( fileName, dZone );
              }
//              else if( fileName.endsWith(".mdb")){
//                  DBAccessor dba = new DBAccessor();
//                  cntr = dba.importZoneFromProject( fileName, dZone );
//              }
              else  {
                  JOptionPane.showMessageDialog(mainFrame,
                      "Wrong File Type. Please select (*.pad) files.");
                  mainFrame.getContentPane().setCursor(
                      Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                  return;
              }

              if( cntr > 0){
                  // Reset current shading
                  mainFrame.menuBar.setCurrentShadingOption(Sys.prjInfo.shadingCat);
                  mainFrame.menuBar.zoomAll();
              }
              JOptionPane.showMessageDialog(mainFrame,
                      "Total "+cntr+ "components imported.");
          } catch (Exception e) {
              JOptionPane.showMessageDialog(mainFrame,
                  "Error occured when importing zones\n Error="+e.toString());
          } finally {
              mainFrame.mainPanel.needRedraw = true;
              mainFrame.mainPanel.repaint();
              mainFrame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
          }
      }//end of btnImport
  }//end of actionPerformed

}//end of class ImportZonesDlg